/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.util;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-21
 * 
 */
public class Util {
	public static final int pageLimit = 10;//maxium page num of available relation list
	public static String trimString(String inStr, int maxLength) {
		if (inStr.length() > maxLength) {
			return inStr.substring(0, maxLength) + "...";
		}
		return inStr;
	}

	public static int getDayNOinWeek(Calendar in) {
		Calendar tmp = in;
		if (tmp == null) {
			tmp = Calendar.getInstance();
		}
		int weekDay = tmp.get(Calendar.DAY_OF_WEEK);
		return weekDay - 1;
	}

	public static int getDayNOinWeek() {
		return getDayNOinWeek(null);
	}

	public static String getShortenName(String uri) {
		String res = "";
		int index = -1;
		if ((index = uri.lastIndexOf("#")) > -1) {
			res = uri.substring(index + 1);
		} else if ((index = uri.lastIndexOf("/")) > -1) {
			res = uri.substring(index + 1);
		} else if ((index = uri.lastIndexOf(":")) > -1) {
			res = uri.substring(index + 1);
		}
		if (res.equals("")) {
			int length = uri.length();
			if (length <= 30) {
				return uri;
			}
			return uri.substring(0, 30) + "...";
		}
		return res;
	}

	public static String getShortenLiteral(String literal) {
		int length = literal.length();
		if (length < 100) {
			return literal;
		}
		return literal.substring(0, 40);
	}

	public static String getLocalname(String uri) {
		int hashIndex = uri.lastIndexOf("#");
		String ret = "";
		if (hashIndex > -1) {
			ret = uri.substring(hashIndex + 1);
		}
		if (ret.equals("")) {
			int slashIndex = uri.lastIndexOf("/");
			if (slashIndex > -1) {
				ret = uri.substring(slashIndex + 1);
			}
		}
		if (ret.equals("")) {
			ret = uri;
		}
		return ret;
	}

	public static void main(String[] argus) throws UnsupportedEncodingException {
		String a = getLocalname("http://www.lifl.fr/~melab/cc");
		System.out.println(a);
	}
}
